package data.email;

import data.Term;
import data.TermCollection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * EmailTermCollection adalah koleksi term dari dokumen email
 * @author Robert Gunawan
 */
public class EmailTermCollection implements TermCollection {

    /**
     * hashmap penampung term dokumen
     */
    private HashMap<String, Term> terms;
    /**
     * jumlah dokumen yang telah diekstrak
     */
    private Integer documentCount;

    /**
     * mendapatkan jumlah unique term dalam label from
     * @return jumlah term
     */
    public Integer getFromTermCount() {
        Integer sumTerm = 0;
        Iterator it = terms.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            Term term = (Term) pairs.getValue();
            if (((EmailStatistic) term.getStatistic()).getFromDF() > 0) {
                sumTerm++;
            }
        }
        return sumTerm;
    }

    /**
     * mendapatkan jumlah unique term dalam label to
     * @return jumlah term
     */
    public Integer getToTermCount() {
        Integer sumTerm = 0;
        Iterator it = terms.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            Term term = (Term) pairs.getValue();
            if (((EmailStatistic) term.getStatistic()).getToDF() > 0) {
                sumTerm++;
            }
        }
        return sumTerm;
    }

    /**
     * mendapatkan jumlah unique term dalam label subject
     * @return jumlah term
     */
    public Integer getSubjectTermCount() {
        Integer sumTerm = 0;
        Iterator it = terms.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            Term term = (Term) pairs.getValue();
            if (((EmailStatistic) term.getStatistic()).getSubjectDF() > 0) {
                sumTerm++;
            }
        }
        return sumTerm;
    }

    /**
     * mendapatkan jumlah unique term dalam label date
     * @return jumlah term
     */
    public Integer getDateTermCount() {
        Integer sumTerm = 0;
        Iterator it = terms.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            Term term = (Term) pairs.getValue();
            if (((EmailStatistic) term.getStatistic()).getDateDF() > 0) {
                sumTerm++;
            }
        }
        return sumTerm;
    }

    /**
     * mendapatkan jumlah unique term dalam label body
     * @return jumlah term
     */
    public Integer getBodyTermCount() {
        Integer sumTerm = 0;
        Iterator it = terms.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            Term term = (Term) pairs.getValue();
            if (((EmailStatistic) term.getStatistic()).getBodyDF() > 0) {
                sumTerm++;
            }
        }
        return sumTerm;
    }

    /**
     * mendapatkan jumlah unique term dalam label union
     * @return jumlah term
     */
    public Integer getUnionCount() {
        Integer sumTerm = 0;
        Iterator it = terms.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            Term term = (Term) pairs.getValue();
            if (((EmailStatistic) term.getStatistic()).getUnionDF() > 0) {
                sumTerm++;
            }
        }
        return sumTerm;
    }

    public EmailTermCollection() {
        terms = new HashMap<String, Term>();
    }

    @Override
    public void addTerm(Term term) {
        term.setCollectionRef(this);
        terms.put(term.getTermString(), term);
    }

    @Override
    public boolean isExists(Term term) {
        return terms.containsKey(term.getTermString());
    }

    @Override
    public synchronized void merge(Term term) {
        Term existingTerm = (Term) terms.get(term.getTermString());
        existingTerm.getStatistic().merge(term.getStatistic());
    }

    @Override
    public Term getExistingTerm(String termString) {
        return (Term) terms.get(termString);
    }

    @Override
    public HashMap<String, Term> getAllTerms() {
        return terms;
    }

    @Override
    public Integer getDocumentCount() {
        return documentCount;
    }

    @Override
    public void setDocumentCount(Integer documentCount) {
        this.documentCount = documentCount;
    }
}
